import time
import unittest

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait


class TestRegister(unittest.TestCase):

    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.get('https://mail.163.com/register/index.htm?from=126mail&utm_source=126mail')
        self.result = ''
        self.target = ''
        self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[5]/span').click()
        self.username = self.browser.find_element_by_id('username')
        self.password = self.browser.find_element_by_id('password')
        self.phone = self.browser.find_element_by_id('phone')
        self.submitbtn = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[6]/a[1]')

    # # 测试注册成功的用例
    # def test_register01(self):
    #     self.username.send_keys('mygenchtest2110153')
    #     self.password.send_keys('Qq1729256881')
    #     self.phone.send_keys('18018650806')
    #     self.submitbtn.click()
    #     # 等待注册成功页面
    #     WebDriverWait(self.browser, 120, 0.5).until(lambda browser: self.browser.find_element_by_xpath('/html/body/div[2]/div/p'))
    #     self.target = 'mygenchtest2110153@126.com 注册成功！'
    #     self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/p').text
    #     self.assertEqual(self.result, self.target, '测试未通过')

    # 测试用户名错误的用例
    def test_register02(self):
        self.username.send_keys('')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '邮箱地址不能为空'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register03(self):
        self.username.send_keys('gench')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '长度应为6～18个字符'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register04(self):
        self.username.send_keys('qwerty12345678912312')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '长度应为6～18个字符'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register05(self):
        self.username.send_keys('2110153genchauto')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '邮箱地址需以字母开头'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register06(self):
        self.username.send_keys('gench2110153')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        time.sleep(2)
        self.target = '该邮箱地址已被占用或不可注册'
        self.result = self.browser.find_element_by_xpath(
            '/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register07(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('')
        self.phone.send_keys('18018523984')
        time.sleep(1)
        self.submitbtn.click()
        self.target = '密码不能为空'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[2]/div').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register08(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('zxcqweasd123456789')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '密码长度应为8-16个字符'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[2]/div').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register09(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('12345678')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '密码需包含“大小写字母、数字、标点符号”中3种以上的组合'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[2]/div').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register10(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('qq1729256881')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '密码需包含“大小写字母、数字、标点符号”中3种以上的组合'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[2]/div').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register11(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('__1729256881')
        self.phone.send_keys('18018523984')
        self.submitbtn.click()
        self.target = '密码需包含“大小写字母、数字、标点符号”中3种以上的组合'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[2]/div').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register12(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('')
        time.sleep(1)
        self.submitbtn.click()
        self.target = '手机号不能为空'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[3]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register13(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('qweqrqw')
        self.submitbtn.click()
        self.target = '请填写正确的中国大陆地区手机号，其他地区手机号请点击此处'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[3]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def test_register14(self):
        self.username.send_keys('mytestgench2110153')
        self.password.send_keys('Qq1729256881')
        self.phone.send_keys('2110153')
        self.submitbtn.click()
        self.target = '请填写正确的中国大陆地区手机号，其他地区手机号请点击此处'
        self.result = self.browser.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[3]/div[2]').text
        self.assertEqual(self.result, self.target, '测试未通过')

    def tearDown(self):
        self.browser.close()


if __name__ == '__main__':
    unittest.main()
